package com.joeone.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.joeone.entity.mti.Member;
import com.joeone.mapper.clickhouse.MemberDetailMapper;
import com.joeone.mapper.mti.MemberMapper;
import com.joeone.request.JoeoneRequest;
import com.joeone.service.MemberService;
import com.joeone.utils.DecimalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
/**
* @author 郑伟杰
* 描述：会员列表-查询方法
* 创建时间 2024/3/18 8:55
*/
@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements MemberService {
    private final MemberMapper memberMapper;
    private final MemberDetailMapper memberDetailMapper;
    @Override
    public List<Member> queryMember(JoeoneRequest joeoneRequest) {
        String dateType = joeoneRequest.getDateType();
        String period = joeoneRequest.getPeriod();
        String jyxz = joeoneRequest.getJyxz();
        List<String> comp = joeoneRequest.getComp();
        List<Member> memberList = new ArrayList<>();
        JSONObject jsonObject = memberMapper.queryMember(dateType, period,jyxz,comp);
        if(null!=jsonObject){
            Member member_xkcjrs = new Member();
            member_xkcjrs.setIndex("新客成交人数");
            member_xkcjrs.setTarget(jsonObject.getBigDecimal("newValue")==null?"/":jsonObject.getBigDecimal("newValue").toString());
            JSONObject newCustomer = memberDetailMapper.queryCjrs(dateType, period, jyxz, comp, "isNewCustomer","");
            setMember(member_xkcjrs, newCustomer);
            memberList.add(member_xkcjrs);

            Member member_lkcjrs = new Member();
            member_lkcjrs.setIndex("老客成交人数");
            member_lkcjrs.setTarget(jsonObject.getBigDecimal("oldValue")==null?"/":jsonObject.getBigDecimal("oldValue").toString());
            JSONObject oldCustomer = memberDetailMapper.queryCjrs(dateType, period, jyxz, comp, "","");
            setMember(member_lkcjrs, oldCustomer);
            memberList.add(member_lkcjrs);

            Member member_hjcjrs = new Member();
            member_hjcjrs.setIndex("合计成交人数");
            member_hjcjrs.setTarget(jsonObject.getBigDecimal("total")==null?"/":jsonObject.getBigDecimal("total").toString());
            JSONObject allCustomer = memberDetailMapper.queryCjrs(dateType, period, jyxz, comp, "","isALl");
            setMember(member_hjcjrs, allCustomer);
            memberList.add(member_hjcjrs);

            Member member_fgl = new Member();
            member_fgl.setIndex("复购率");
            member_fgl.setTarget("/");
            JSONObject fgl = memberDetailMapper.queryFgl(dateType, period, jyxz, comp, "");
            setMember(member_fgl, fgl);
            member_fgl.setRate("/");//达成率=实际/目标，没有目标，所以设为”/“
            memberList.add(member_fgl);

            Member member_xszb90 = new Member();
            member_xszb90.setIndex("90后销售占比");
            member_xszb90.setTarget(jsonObject.getBigDecimal("total")==null?"/":jsonObject.getBigDecimal("total").toString());
            JSONObject sales90 = memberDetailMapper.querySales90(dateType, period, jyxz, comp, "");
            if(null!= sales90){
                BigDecimal bq = sales90.getBigDecimal("bq");
                BigDecimal bq90 = sales90.getBigDecimal("bq90");
                BigDecimal tq = sales90.getBigDecimal("tq");
                BigDecimal tq90 = sales90.getBigDecimal("tq90");
                if(null!=bq90 && bq90.compareTo(BigDecimal.ZERO)!=0) {
                    member_xszb90.setActual(bq.divide(bq90, 4, RoundingMode.UP));
                }
                if(null!=tq90 && tq90.compareTo(BigDecimal.ZERO)!=0) {
                    member_xszb90.setSynchronous(tq.divide(tq90, 4, RoundingMode.UP));
                }
                member_xszb90.setRate("/");
                //同比=本期-同期
                BigDecimal actual = member_xszb90.getActual()==null?BigDecimal.ZERO:member_xszb90.getActual();
                BigDecimal synchronous = member_xszb90.getSynchronous()==null?BigDecimal.ZERO:member_xszb90.getSynchronous();
                member_xszb90.setYearOnYear(DecimalUtil.subtractDecimal(actual,synchronous));
            }
            memberList.add(member_xszb90);
        }
        return memberList;
    }

    private Member setMember(Member member_fgl, JSONObject fgl) {
        if(null!= fgl){
            BigDecimal bq = fgl.getBigDecimal("bq");
            BigDecimal tq = fgl.getBigDecimal("tq");
            BigDecimal target = member_fgl.getTarget()=="/"?BigDecimal.ZERO:new BigDecimal(member_fgl.getTarget());
            member_fgl.setActual(bq);
            member_fgl.setSynchronous(tq);
            member_fgl.setRate(DecimalUtil.divideDecimal(bq,target)==null?"/": DecimalUtil.divideDecimal(bq,target).toString()); // 达成率
            member_fgl.setYearOnYear(DecimalUtil.divideDecimal(DecimalUtil.subtractDecimal(bq,tq),tq));//同比=(本期-同期)/同期
        }
        return member_fgl;
    }
}
